package com.goldenautumn.prefabricate.activity.home

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.category.CategoryLayoutAdapter
import com.goldenautumn.prefabricate.activity.mall.GoodsDetailsActivity
import com.goldenautumn.prefabricate.business.entity.GoodsInfoVO
import com.goldenautumn.prefabricate.business.entity.GoodsSkuVo
import com.goldenautumn.prefabricate.business.entity.HomeItemDataVo
import com.goldenautumn.prefabricate.business.entity.SearchParams
import com.goldenautumn.prefabricate.business.mall.GoodsInfoUiState
import com.goldenautumn.prefabricate.business.mall.MallViewModel
import com.goldenautumn.prefabricate.databinding.FragmentHomeTabBinding
import com.goldenautumn.prefabricate.utils.Constants
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseFragment
import qncx.external.common.utils.intent

class HomeTabFragment : BaseFragment<FragmentHomeTabBinding>() {

    companion object {
        fun newInstance(type: String,map:String): HomeTabFragment {
            val fragment = HomeTabFragment()
            val bundle = Bundle()
            bundle.putString(Constants.BUSINESS_PAGE_DATA, type)
            bundle.putString("DATA", map)
            fragment.arguments = bundle
            return fragment
        }
    }

    private val mMallModel by viewModels<MallViewModel>()

    private val mAdapter by lazy {
        CategoryLayoutAdapter()
    }

    private val type: String?
        get() = arguments?.getString(Constants.BUSINESS_PAGE_DATA)

    private val data: String?
        get() = arguments?.getString("DATA")

    override fun getLayoutId(): Int = R.layout.fragment_home_tab

    override suspend fun initView() {
        mBinding.typeRv.layoutManager = StaggeredGridLayoutManager(2, RecyclerView.VERTICAL)
        mBinding.typeRv.adapter = mAdapter

        val mapData:MutableList<HomeItemDataVo> = GsonUtils.fromJson(data,object : TypeToken<MutableList<HomeItemDataVo>>() {}.type)

        val datas = ArrayList<GoodsInfoVO>()
        mapData.forEach {
            datas.add(GoodsInfoVO(
                goods_id = "${it.goods_id}".toInt(),
                goods_image = it.goods_image,
                goods_name = it.goods_name,
                goods_sku = GoodsSkuVo(goods_price = it.goods_price),
                ))
        }

        mAdapter.submitList(datas)

//        mMallModel.getGoodsList(SearchParams(isSpecial = type))

        mAdapter.setOnItemClickListener{ adapter,_,position ->
            val item = adapter.getItem(position)
            startActivity(GoodsDetailsActivity::class.intent.putExtra(Constants.BUSINESS_PAGE_DATA,item?.goods_id))
        }

        while (isActive) {
            select {
                mMallModel.goodsDetailsRequests.onReceive {
                    when(it) {
                        is GoodsInfoUiState.GoodsList -> {
                            mAdapter.submitList(it.goodsList)
                        }
                        else -> {}
                    }
                }
            }
        }
    }


}